package com.goldman.aplt.util;

import com.goldman.oss.common.FileStorageProcessorFactory;
import com.goldman.oss.common.internal.pojo.UploadFileInfo;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRPdfExporterParameter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ReportUtil {

    public final static String TAGRET_PATH = "/data/file/reportTemp/temp/";
    private final static String FILE_PATH = "goldman/app/report/bill/";
    private final static String PROTOCOL_PATH = "goldman/app/report/protocol/";

    public static String genProtocol(String tempPath, Map tempParam, String targetFileName) throws IOException, JRException {
        JasperPrint temp = JasperFillManager.fillReport(tempPath, tempParam, new JREmptyDataSource());
        //输出文件
        String pdfTargetPath = TAGRET_PATH+targetFileName;
        List<JasperPrint> jprintlist = new ArrayList();
        jprintlist.add(temp);
        JRExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, jprintlist);
        File pdf = new File(pdfTargetPath);
        OutputStream output = new FileOutputStream(pdf);
        exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, output);
        exporter.exportReport();

        UploadFileInfo uploadFileInfo = FileStorageProcessorFactory.ofUpload(pdf)
                .setPath(PROTOCOL_PATH)
                .setFilename(targetFileName)
                .setBusinessCode("obs-goldman-biz")
                .uploadFile();
        //上传成功 删除本地文件
        pdf.delete();
        return uploadFileInfo.getObjectKey();
    }
    public static String genBillReport(String coverTempPath, String tempPath, Map coverParam, Map tempParam, List<Map> tempTable, String targetFileName) throws IOException, JRException {
        JasperPrint cover = JasperFillManager.fillReport(coverTempPath, coverParam);
        JasperPrint temp = JasperFillManager.fillReport(tempPath, tempParam, new JRBeanCollectionDataSource(tempTable));
        //输出文件
        String pdfTargetPath = TAGRET_PATH+targetFileName;
        List<JasperPrint> jprintlist = new ArrayList();
        jprintlist.add(cover);
        jprintlist.add(temp);
        JRExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, jprintlist);
        File pdf = new File(pdfTargetPath);
        OutputStream output = new FileOutputStream(pdf);
        exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, output);
        exporter.exportReport();

        UploadFileInfo uploadFileInfo = FileStorageProcessorFactory.ofUpload(pdf)
                .setPath(FILE_PATH)
                .setFilename(targetFileName)
                .setBusinessCode("obs-goldman-biz")
                .uploadFile();
        //上传成功 删除本地文件
        pdf.delete();
        return uploadFileInfo.getObjectKey();
    }
}
